package com.xm.xmwa1706090.service.netty.client;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletException;

/**
 * @Company 杭州新苗网络科技有限公司
 * @Author : loysen
 * @DateTime：2017年4月22日 下午5:54:15
 *
 * @Description : {Netty初始化类}
 */
@Service
public class NettyClientInitializer extends ChannelInitializer<SocketChannel> {

	Logger logger = LoggerFactory.getLogger(NettyClientInitializer.class );

	private DispatcherServlet dispatcherServlet;

	public NettyClientInitializer() throws ServletException{
			logger.info("  nettyClient start up success .");
			System.out.println(" nettyClient start up success .");
	}

	@Override
	public void initChannel(SocketChannel ch) {
		ChannelPipeline pipeline = ch.pipeline();
		pipeline.addLast(new IdleStateHandler(30,30,0));
		pipeline.addLast(new ObjectEncoder());
		pipeline.addLast(new ClientSocketEntityDecoder());
		pipeline.addLast(new NettyClientHandler());
		pipeline.addLast("logging",new LoggingHandler(LogLevel.DEBUG));
		pipeline.addLast(new NettyClientHandler());
	}
}
